package org.pentaho.di.ui.spoon.navigation.design.classification.plugin;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.LaniusPluginVersionTreeItem;
import org.pentaho.di.ui.core.gui.GUIResource;
import org.pentaho.di.ui.spoon.navigation.Bundle;
import org.pentaho.di.ui.spoon.navigation.design.classification.category.Category;

import java.util.*;

/**
 * @author yzbdl@yzbdl.ac.cn
 * @version: 1.0
 * @Date 2020/10/15 14:09
 **/
public class PluginVersion  extends Bundle{

//	// 每页显示记录数
//	public static final Integer pageRecords = 10;

    /**
     * data
     */
    private PluginVersionEntity pluginVersionEntity;

    private Set<Plugin> plugins = new LinkedHashSet<>();

    private Map<Integer, Plugin> pluginsMap = new LinkedHashMap<>();

//	// 当前页码
//	private Integer currentPage;
//	// 总页码
//	private Integer totalPage;
//	// 是否分页
//	private Boolean isPagable;

    /**
     * view
     */
    private Bundle parent;

    private LaniusPluginVersionTreeItem treeItem;

    public PluginVersion(Bundle parent, PluginVersionEntity pluginVersionEntity) {
        super(parent.getSpoon());
        this.parent = parent;
        this.pluginVersionEntity = pluginVersionEntity;
        this.treeItem = new LaniusPluginVersionTreeItem(((Category) parent).getTreeItem(), SWT.NONE);
        this.treeItem.setText(pluginVersionEntity.getName());
        this.treeItem.setImage(GUIResource.getInstance().getPluginFolderImage());
        this.treeItem.setExpanded(isExpanded(pluginVersionEntity.getExpanded()));
        this.treeItem.setPluginVersionEntity(pluginVersionEntity);
    }

    public boolean isEmpty() {
        return this.plugins.isEmpty();
    }

    public void dispose() {
        this.treeItem.dispose();
    }

    public void setPlugins(Set<Plugin> plugins) {
        this.plugins = plugins;
        for (Plugin plugin : plugins) {
            this.pluginsMap.put(plugin.getId(), plugin);
        }
    }

    public Bundle getParent() {
        return parent;
    }

    public void setParent(Bundle parent) {
        this.parent = parent;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        PluginVersion pluginVersion = (PluginVersion) o;
        return Objects.equals(pluginVersionEntity, pluginVersion.pluginVersionEntity);
    }

    /**
     * 获取pluginVersionEntity
     *
     * @return pluginVersionEntity
     */
    public PluginVersionEntity getPluginVersionEntity() {
        return pluginVersionEntity;
    }

    public void setPluginVersionEntity(PluginVersionEntity pluginVersionEntity) {
        this.pluginVersionEntity = pluginVersionEntity;
    }

    /**
     * 获取plugins
     *
     * @return plugins
     */
    public Set<Plugin> getPlugins() {
        return plugins;
    }

    /**
     * 获取pluginsMap
     *
     * @return pluginsMap
     */
    public Map<Integer, Plugin> getPluginsMap() {
        return pluginsMap;
    }

    public void setPluginsMap(Map<Integer, Plugin> pluginsMap) {
        this.pluginsMap = pluginsMap;
    }

    /**
     * 获取treeItem
     *
     * @return treeItem
     */
    public LaniusPluginVersionTreeItem getTreeItem() {
        return treeItem;
    }

    public void setTreeItem(LaniusPluginVersionTreeItem treeItem) {
        this.treeItem = treeItem;
    }

    @Override
    public int hashCode() {
        return Objects.hash(pluginVersionEntity);
    }
}
